package com.tarena.day02;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class Demo01 {
	 public static void main(String[] args) throws Exception{
		 JFrame w = new JFrame("飞扬的小鸟");//把类变成对象
         w.setSize(432,674);
         MyPanel m= new MyPanel();
         w.add(m);
		 w.setVisible(true);//设置 可见 真的
		 m.run();
	 }

}

//继承类
class MyPanel extends JPanel
{  //int a;
   Ground ground;
   Bird bird;
   BufferedImage background;
   Column column1;
   Column column2; 
   int score;//分数
   /*添加游戏开始状态*/
   boolean started;
   BufferedImage startImage;
   /*添加游戏结束状态*/
   boolean gameOver;
   BufferedImage gameOverImage;
   //构造方法
   public MyPanel() throws Exception{
	   score=0;
	   ground =new Ground();
	   bird=new Bird();
	   column1 =new Column(1);//第一根柱子
	   column2 =new Column(2);//第二根柱子
	   started=false;
	   startImage= ImageIO.read(
			   getClass().getResource("start.png"));
	   gameOver=false;
	   gameOverImage= ImageIO.read(
			   getClass().getResource("gameover.png"));
	   background= ImageIO.read(
		   getClass().getResource("bg.png"));
   }
   public void paint(Graphics g){//画图中的坐标是左上角的坐标
		   g.drawImage(background,0,0,null);
		   g.drawImage(column1.image,column1.x-column1.width/2,column1.y-column1.height/2,null);
		   g.drawImage(column2.image,column2.x-column2.width/2,column2.y-column2.height/2,null);
		   
		   g.drawImage(ground.image,ground.x,ground.y,null);
		   Font f = new Font(Font.SANS_SERIF,Font.BOLD,40);
		   g.setFont(f);
		   g.drawString(""+score,20,40);
		   g.setColor(Color.WHITE);
		   g.drawString(""+score,20-3,40-3);
		   Graphics2D g2 = (Graphics2D)g;
		   g2.rotate(-bird.a1pha,bird.x,bird.y);
		   g.drawImage(bird.image,bird.x-bird.width,bird.y-bird.height,null);
		   g2.rotate(bird.a1pha,bird.x,bird.y);
		   if (gameOver){
			   g.drawImage(gameOverImage,0,0,null);
		   }
		   if (!started){
			   g.drawImage(startImage,0,0,null);
		   }     
	   }

  public void run()throws Exception{
	  MouseListener l= new MouseAdapter(){
		  public void mousePressed(MouseEvent e){
			  try{
				  if(gameOver){
				  column1 =new Column(1);
				   column2 =new Column(2);
				  bird = new Bird();
				  started = false;
				  gameOver = false;
				  score=0;	  
			  } else{
				  started = true;
				  bird.flappy();
			  }  
		  }catch(Exception ex){}
	  }
	  };
	  addMouseListener(l);
	//死循环,如果没有结束，就走一步
	  while(true){
		  if(!gameOver){
			  if(started){
				  bird.step();
				  column1.step();
				  column2.step();  
			  }
			  bird.fly();
			  ground.step();
		  }
		  //记分逻辑
		  if(bird.x==column1.x||bird.x==column2.x){
			  score+=100;
		  }
		  //如果鸟撞上柱子和地面，游戏结束
		  //如果返回false则发生碰撞
		  if(bird.hit(column1)||bird.hit(column2)||bird.hit(ground)){
			  gameOver=true;
		  }
			  Thread.sleep(10);
			  repaint();//重画
		   }
		   }
		   
	   }

class Ground{
	BufferedImage image;
	int x,y;
	public Ground() throws Exception{
		image= ImageIO.read(
			   getClass().getResource("ground.png"));
	           x=0;y=500;
	}
	public void step(){
		x--;
		if(x==-108){
			  x = 0;
			  }
		}
}

class Column{
	BufferedImage image;
	int x,y;//中心点的坐标
	int width,height;
	int gap;//柱子中间缝隙的高度
	int distance;//两根柱子的间距
	Random r =new Random();//132~350
	public Column(int n) throws Exception{
		image= ImageIO.read(
	   					   getClass().getResource("column.png"));
		width= image.getWidth();
		height= image.getHeight();
		gap=144;
		distance=245;
		x = 550+(n-1)*distance;
		y = r.nextInt(218)+132;
}
    public void step(){
	x--;
	if(x==-width/2){
		  x = distance*2-width/2;
		  y = r.nextInt(218)+132;
		  }
	}
}

class Bird{
	BufferedImage image;
	BufferedImage[] images;//数组
	int index;//数组下标
	int x,y;//小鸟中心点的坐标
	int width,height;
	int size;//鸟的大小，用于碰撞检测
	//s=v0t-g*t*t/2
	double g,t,v0,s;
	double speed;//当前的上抛速度
	double a1pha;//鸟的倾斜角度
	
	public Bird() throws Exception{
		x=130;
		y=280;
		
		size=40;
		g=4;
		t=0.25;
		s=0;
		a1pha = 0;
		v0=20;
		image= ImageIO.read(
				getClass().getResource("0.png"));
		images =new BufferedImage[8];
		for(int i=0;i<8;i++){
			images[i]=ImageIO.read(
					getClass().getResource(""+i+".png"));
			index=0;
			width=image.getWidth();
			height=image.getHeight();
		}
		
	}
	public void fly(){
		index++;
		image = images[index/4%8];
		
	}
	public void step(){
		double v0= speed;
		s=v0*t-g*t*t/2;//计算位移
		y=y-(int)s;//计算鸟的坐标位置
		double v=v0-g*t;//计算下次速度
		speed=v;
		a1pha = Math.atan(s/8);	
	}
	public void flappy(){
		speed = v0;
	}
	//用hit方法来检测碰撞检测
	public boolean hit(Ground ground){
		boolean hit=y+size/2>ground.y;
		return hit;
	}
	public boolean hit(Column column){
		//检测柱子是否在区域
		if(x<column.x+column.width/2+size/2&&x>column.x-column.width/2-size/2){
			//检测是否在柱子的空白区域
			if(y<column.y+column.gap/2-size/2&&y>column.y-column.gap/2+size/2){
				return false;
			}
				return true;
		}
		return false;
	}
}
